package com.derbysoft.dswitch.adapter.ihg.dswitch3.translate.impl;

import com.derbysoft.dswitch.adapter.ihg.dto.OTAHotelAvailRS;
import com.derbysoft.dswitch.adapter.ihg.dswitch3.translate.Translator;
import com.derbysoft.dswitch.dto.hotel.avail.HotelAvailRS;
import com.derbysoft.dswitch.dto.hotel.avail.HotelAvailRoomStayDTO;
import com.derbysoft.dswitch.remote.hotel.dto.HotelAvailRequest;
import com.derbysoft.dswitch.remote.hotel.dto.HotelAvailResponse;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by: jason
 * Date: 2012-5-10
 */
@Component("hotelAvailRSTranslator")
public class HotelAvailRSTranslatorImpl extends BaseTranslator
        implements Translator<OTAHotelAvailRS, HotelAvailResponse, HotelAvailRequest> {

    @Override
    public HotelAvailResponse translate(OTAHotelAvailRS otaHotelAvailRS, HotelAvailRequest request) {
        HotelAvailResponse hotelAvailResponse = new HotelAvailResponse();
        hotelAvailResponse.setTaskId(request.getHeader().getTaskId());
        hotelAvailResponse.setHotelAvailRS(createHotelAvailRS(otaHotelAvailRS, request));
        return hotelAvailResponse;
    }

    private HotelAvailRS createHotelAvailRS(OTAHotelAvailRS otaHotelAvailRS, HotelAvailRequest request) {
        HotelAvailRS hotelAvailRS = new HotelAvailRS();
        List<HotelAvailRoomStayDTO> hotelAvailRoomStayDTOs = new ArrayList<HotelAvailRoomStayDTO>();
        hotelAvailRoomStayDTOs.add(createAvailRoomStayDTO(otaHotelAvailRS, request));
        hotelAvailRS.setHotelAvailRoomStaysList(hotelAvailRoomStayDTOs);
        if (hotelAvailRS.getHotelAvailRoomStaysList().isEmpty()) {
            hotelAvailRS.setNoAvailHotelsList(request.getHotelAvailRQ().getAvailCriteria().getHotelCodesList());
        }
        return hotelAvailRS;
    }

    private HotelAvailRoomStayDTO createAvailRoomStayDTO(OTAHotelAvailRS otaHotelAvailRS, HotelAvailRequest request) {
        HotelAvailRoomStayDTO hotelAvailRoomStayDTO = new HotelAvailRoomStayDTO();
        hotelAvailRoomStayDTO.setHotelCode(request.getHotelAvailRQ().getAvailCriteria().getHotelCodesList().get(0));
        hotelAvailRoomStayDTO.setRoomStaysList(createRoomStays(otaHotelAvailRS, request));
        return hotelAvailRoomStayDTO;
    }
}
